
$(document).ready(function () {

    $(".main_visual").hover(function () {
        $("#btn_prev,#btn_next").fadeIn()
    }, function () {
        $("#btn_prev,#btn_next").fadeOut()
    });

    // 轮播图部分start
    $dragBln = false;
    $(".main_image").touchSlider({
        flexible: true,
        speed: 200,
        btn_prev: $("#btn_prev"),
        btn_next: $("#btn_next"),
        paging: $(".flicking_con a"),
        counter: function (e) {
            $(".flicking_con a").removeClass("on").eq(e.current - 1).addClass("on");
        }
    });

    $(".main_image").bind("mousedown", function () {
        $dragBln = false;
    });

    $(".main_image").bind("dragstart", function () {
        $dragBln = true;
    });

    $(".main_image a").click(function () {
        if ($dragBln) {
            return false;
        }
    });

    timer = setInterval(function () {
        $("#btn_next").click();
    }, 3000);

    $(".main_visual").hover(function () {
        clearInterval(timer);
    }, function () {
        clearInterval(timer);
        timer = setInterval(function () {
            $("#btn_next").click();
        }, 3000);
    });

    $(".main_image").bind("click", function () {
        clearInterval(timer);
    }).bind("touchend", function () {
        clearInterval(timer);
        timer = setInterval(function () {
            $("#btn_next").click();
        }, 3000);
    });

    // end

    $(".homepage_list>li").on("click", function () {
        var novelid = $(this).attr('novel-id');
        location.href = "../novel/novelInfo?novelid=" + novelid;
    })
    //查看更多分类
    $(".homepage_list>h2>span").on("click", function (e) {
        var e = e || event;
        e.stopPropagation();
        var type = $(this).attr('data-type');
        var from = $(this).attr('data-from');
        location.href = "../novel/" + type + "?from="+from
    })


    // 我的书架
    $(".new_read>button").on("click", function () {
        location.href = "../user/myBookcase?type=1"
    })



//悬浮小图标效果
// »ñÈ¡½Úµã
    var block = document.getElementById("block");
    var oW, oH;
    // °ó¶¨touchstartÊÂ¼þ
    block.addEventListener("touchstart", function (e) {
        console.log(e);
        $("html").css("overflow", "hidden");
        var touches = e.touches[0];
        oW = touches.clientX - block.offsetLeft;
        oH = touches.clientY - block.offsetTop;
        //×èÖ¹Ò³ÃæµÄ»¬¶¯Ä¬ÈÏÊÂ¼þ
        document.addEventListener("touchmove", defaultEvent, false);
    }, false)

    block.addEventListener("touchmove", function (e) {
        var touches = e.touches[0];
        var oLeft = touches.clientX - oW;
        var oTop = touches.clientY - oH;
        if (oLeft < 0) {
            oLeft = 0;
        } else if (oLeft > document.documentElement.clientWidth - block.offsetWidth) {
            oLeft = (document.documentElement.clientWidth - block.offsetWidth);
        }
        if (oTop < 0) {
            oTop = 0
        } else if (oTop > document.documentElement.clientHeight - block.offsetHeight) {
            oTop = (document.documentElement.clientHeight - block.offsetHeight);
        }
        block.style.left = oLeft + "px";
        block.style.top = oTop + "px";
    }, false);

    block.addEventListener("touchend", function () {
        document.removeEventListener("touchmove", defaultEvent, false);
        $("html").css("overflow", "auto")
    }, false);
    function defaultEvent(e) {
        e.preventDefault();
    }




    //最近阅读快捷方式
    $("aside").on("click", function () {
        $(".aside_Info").css("visibility", "visible").css("transition", "none").css("background", "rgba(0,0,0,0.4)");
        $(".aside_Info>div").css("transform", "translateX(0)");
        $(".aside_Info>ul").css("transform", "translateX(0)");
        $("html").css("overflow", "hidden")
        $(".aside_Info").on("click", function () {
            $(".aside_Info").css("visibility", "hidden");
            $(".aside_Info>div").css("transform", "translateX(14.02rem)");
            $(".aside_Info>ul").css("transform", "translateX(14.02rem)");
            $("html").css("overflow", "auto")
        })
    })

    $(".talk_login").bind("click", function (e) {
        var e = e || event;
        e.stopPropagation();
        location.href = "../user/login"
    })

    // 顶部导航链接
    var top_nav_arr = ["../html/newBook.html?new", "../html/billboard.html", "../html/classifty.html", "../html/newBook.html?free"];
    $("nav>li").on("click", function () {
        location.href = top_nav_arr[$(this).index()]
    })
    // 底部导航链接
    var but_nav_arr = ["../html/homepage.html", "../html/classifty.html", "../html/billboard.html", "../html/newBook.html?new", "../html/newBook?free"]
    $(".button_nav>li").on("click", function () {
        location.href = but_nav_arr[$(this).index()]
    })

    //悬浮小图标效果
    var block = document.getElementById("block");
    var oW, oH;
    block.addEventListener("touchstart", function (e) {
        $("html").css("overflow", "hidden");
        var touches = e.touches[0];
        oW = touches.clientX - block.offsetLeft;
        oH = touches.clientY - block.offsetTop;

        document.addEventListener("touchmove", defaultEvent, false);
    }, false)

    block.addEventListener("touchmove", function (e) {
        var touches = e.touches[0];
        var oLeft = touches.clientX - oW;
        var oTop = touches.clientY - oH;
        if (oLeft < 0) {
            oLeft = 0;
        } else if (oLeft > document.documentElement.clientWidth - block.offsetWidth) {
            oLeft = (document.documentElement.clientWidth - block.offsetWidth);
        }
        if (oTop < 1.13 * 46.875) {
            oTop = 1.13 * 46.875
        } else if (oTop > document.documentElement.clientHeight - block.offsetHeight) {
            oTop = (document.documentElement.clientHeight - block.offsetHeight);
        }
        block.style.left = oLeft + "px";
        block.style.top = oTop + "px";
    }, false);

    block.addEventListener("touchend", function () {
        document.removeEventListener("touchmove", defaultEvent, false);
        $("html").css("overflow", "auto");
        if (block.offsetLeft < (document.documentElement.clientWidth - block.offsetWidth) / 2 && block.offsetLeft >= 20) {
            $(block).animate({'left': 0}, 200, function () {
                $(block).animate({"left": "20px"}, 150, function () {
                    $(block).animate({"left": 0}, 100, function () {
                        $(block).animate({"left": "10px"}, 80, function () {
                            $(block).animate({"left": 0}, 80)
                        })
                    })
                })
            });
        } else if (block.offsetLeft > (document.documentElement.clientWidth - block.offsetWidth) / 2 && block.offsetLeft < document.documentElement.clientWidth - block.offsetWidth - 15) {
            $(block).animate({'left': (document.documentElement.clientWidth - block.offsetWidth) + "px"}, 200, function () {
                $(block).animate({'left': (document.documentElement.clientWidth - block.offsetWidth - 20) + "px"}, 150, function () {
                    $(block).animate({"left": (document.documentElement.clientWidth - block.offsetWidth) + "px"}, 100, function () {
                        $(block).animate({"left": (document.documentElement.clientWidth - block.offsetWidth - 10) + "px"}, 80, function () {
                            $(block).animate({"left": (document.documentElement.clientWidth - block.offsetWidth) + "px"}, 80)
                        })
                    })
                })
            });
        } else if (block.offsetLeft < 20) {
            block.style.left = 0;
        } else {
            block.style.left = (document.documentElement.clientWidth - block.offsetWidth) + "px";
        }
    }, false);
    function defaultEvent(e) {
        e.preventDefault();
    }
});
